#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/buffer.h>
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>
#include <linux/sysfs.h>

#include "ms518xn.h"

static int ms518xn_spi_probe(struct spi_device *spi)
{
	struct iio_dev* indio_dev;
	struct device_node* node = spi->dev.of_node;

	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(ms518xn_t));

	if (indio_dev == NULL)
		goto err_iio_alloc;

	if (spi_setup(spi) != 0)
		goto err_spi_setup;

	if (ms518xn_probe(indio_dev, node, MS518XN_USE_SPI_PHY, spi) != 0)
		goto err_ms518xn_probe;

	spi_set_drvdata(spi, indio_dev);

	return 0;

err_ms518xn_probe:
err_spi_setup:
	iio_device_free(indio_dev);
err_iio_alloc:
	dev_info(&spi->dev, "ERRO: ms518xn-spi probe fail!\n");
	return ENOMEM;
}

static const struct of_device_id ms518xn_of_match[] = {
	{ .compatible = "ruimeng,ms518xn" },
	{ },
};

static struct spi_driver ms518xn_spi_driver = {
	.driver = {
		.name = "ms518xn",
		.of_match_table = ms518xn_of_match,
	},
	.probe = ms518xn_spi_probe,
//	.id_table = ms519x_id_table,
};

MODULE_DEVICE_TABLE(of, ms518xn_of_match);

module_spi_driver(ms518xn_spi_driver);

MODULE_AUTHOR("Yize Niu <niuyize@loongson.cn>");
MODULE_DESCRIPTION("Analog Devices MS519XN ADC");
MODULE_LICENSE("GPL v2");

